<?php

jimport('joomla.application.component.controller');

class bgController extends JController {
    public function display($cachable = false, $urlparams = false) {
        parent::display($cachable, $urlparams);
    }
    
    public function selectArea(){
        $area_code = JRequest::getCmd('area_code',0);
        
        $model = $this->getModel();
        $pref = $model->getAllPrefOfArea($area_code);
        echo '<select name="pref" id="pref" class="medium">';
        echo '<option value="0" selected="selected">全て...</option>"';                             
        foreach($pref as $value){                       
            echo '<option value="'.$value->PREF_CODE.'">'.$value->PREF_NAME.'</option>';        
        }                       
        echo '</select>';
        $app = &JFactory::getApplication();
        $app->close();
        exit();
    }
    
    public function search_user(){
        $filter = array(
            'ocba_name' => JRequest::getVar('ocba_name'),
            'pref_search' => JRequest::getVar('pref_search'),
            'area_search' => JRequest::getVar('area_search'),
            'usertype_search' => JRequest::getVar('usertype_search'),
            );
        $result = $this->getModel()->getAllUserMail($filter);
        if($filter['usertype_search'] == 'cooh'){
          echo '<div class="the_scroll custom">
                        <table style="width: 980px">
                           <tbody>
                              <tr style="
                                      height:30px;
                                      line-height: 30px;
                                      width:100%;
                                      text-align: left;
                                      background-color: #DCDCDC;">
                                  <th style="width: 50px" class="check left"><input  style="margin-top: 8px;" id="toggle" name="toggle" type="checkbox" onclick="checkAll(this);checkID();" /></th>

                                  <th style="width: 150px" class="code left">配信先名</th>
                                  <th style="width: 250px" class="mail left">自宅メールアドレス</th>
                                  <th style="width: 250px" class="m_mail left">携帯メールアドレス</th>
                                  <th style="width: 250px" class="w_mail left">勤務先メールアドレス</th>
              </tr>
                           
                           
                           ';
          if(!empty($result)){
              foreach ($result as $key => $row){
                $cls = 0;
                if((empty($row->email) || $row->mail_flag != 1) && (empty($row->m_email) || $row->m_mail_flag != '1') && (empty($row->w_email) || $row->w_mail_flag != '1')) {
                  continue;
                }
                $mail = $row->mail_flag == '1' ? $row->email : '拒む';
                $m_mail = $row->m_mail_flag == '1' ? $row->m_email : '拒む';
                $w_mail = $row->w_mail_flag == '1' ? $row->w_email : '拒む';
                  echo   '<tr class="row'.$cls.'">
                             <td class="check left" style="width: 50px">
                                  <input class="checkbox-id" type="checkbox" id="cb'.$row->id.'" name="cid[]" value="'.$row->id.'" onclick="isChecked(this.checked);checkID('.$row->id.');" />                           
                             </td>
                             <td style="width: 150px" class="code left" id="user'.$row->id.'">'.$row->name.'</td>
                             <td style="width: 250px" class="left" id="email'.$row->id.'">'.$mail.'</td>
                             <td style="width: 250px" class="left" id="m_email'.$row->id.'">'.$m_mail.'</td>
                             <td style="width: 250px" class="left" id="w_email'.$row->id.'">'.$w_mail.'</td>
                          </tr>';
              }
          } else {
              echo   '    <tr>
                              <td class="center" colspan="5">データは見つかりませんでした。</td>
                          </tr>';
          }
          
          echo '          </tbody>         
                      </table>
                        </div>
                        <input type="hidden" id="all_user" name="all_user" value="'.$s.'" />
                        <input type="hidden" name="task" value="" />
                        <input type="hidden" name="boxchecked" id="boxchecked" value="0"/>
                        
                        <textarea name="list_uid" id="list_uid"></textarea>
                        ';
        } else {
          echo '<div class="the_scroll">
                        <table>
                           <thead>
                              <tr>
                                  <th class="check left"><input style="margin-top: 8px;" id="toggle" name="toggle" type="checkbox" onclick="checkAll(this);checkID();" /></th>
                                  <th class="code left">配信先メールアドレス</th>
                                  <th class="mail left">配信先名</th>
                              </tr>
                           </thead>
                           
                           <tbody>';
          if(!empty($result)){
            
              foreach ($result as $key => $row){
                $cls = 0;
                  echo   '<tr class="row'.$cls.'">
                             <td class="check left">
                                  <input class="checkbox-id" type="checkbox" id="cb'.$row->id.'" name="cid[]" value="'.$row->id.'" onclick="isChecked(this.checked);checkID('.$row->id.');" />                           
                             </td>
                             <td class=" code left"id="email'.$row->id.'">'.$row->email.'</td>
                             <td class="left" id="user'.$row->id.'">'.$row->name.' </td>
                          </tr>';
              }
          } else {
              echo   '    <tr>
                              <td class="center" colspan="3">データは見つかりませんでした。</td>
                          </tr>';
          }
          
          echo '          </tbody>         
                      </table>
                        </div>
                        <input type="hidden" id="all_user" name="all_user" value="'.$s.'" />
                        <input type="hidden" name="task" value="" />
                        <input type="hidden" name="boxchecked" id="boxchecked" value="0"/>
                        
                        <textarea name="list_uid" id="list_uid"></textarea>
                        ';
        }
        
        
        
        $app = &JFactory::getApplication();
        $app->close();
    }
    
    public function search_group(){
        $result = $this->getModel()->getAllGroup();
        
        echo '<div class="the_scroll">
                      <table>
                         <thead>
                            <tr>
                                <th class="check left"><input style="margin-top: 8px;" id="toggle" name="toggle" type="checkbox" onclick="checkAll(this);checkID();" /></th>
                                <th class="code left">配信先コード</th>
                                <th class="mail left">配信先名</th>
                            </tr>
                         </thead>
                         
                         <tbody>';
        if(!empty($result)){

            foreach ($result as $key => $row){
              $cls = 0;
                echo   '<tr class="row'.$cls.'">
                           <td class="check left">
                                <input class="checkbox-id" type="checkbox" id="cb'.$row->MAGR_CODE.'" name="cid[]" value="'.$row->MAGR_CODE.'" onclick="isChecked(this.checked);checkID('.$row->MAGR_CODE.');" />                           
                           </td>
                           <td class="code left" id="g_code'.$row->MAGR_CODE.'">'.$row->MAGR_CODE.'</td>
                           <td class="left" id="g_name'.$row->MAGR_CODE.'">'.$row->MAGR_NAME.'</td>
                        </tr>';
            }
        } else {
            echo   '    <tr>
                            <td class="center" colspan="3">データは見つかりませんでした。</td>
                        </tr>';
        }
        
        echo '          </tbody>         
                    </table>
                      </div>
                      <input type="hidden" id="all_user" name="all_user" value="'.$s.'" />
                      <input type="hidden" name="task" value="" />
                      <input type="hidden" name="boxchecked" id="boxchecked" value="0"/>
                      
                      <textarea name="list_uid" id="list_uid"></textarea>
                      ';
        
        $app = &JFactory::getApplication();
        $app->close();
    }
    
    public function search_user_frmessage(){
        $filter = array(
            'ocba_name' => JRequest::getVar('ocba_name'),
            'pref_search' => JRequest::getVar('pref_search'),
            'area_search' => JRequest::getVar('area_search'),
            'usertype_search' => JRequest::getVar('usertype_search'),
            );
        $result = $this->getModel()->getAllUserMailMessage($filter);
        
        echo '<div class="the_scroll">
                      <table>
                         <thead>
                            <tr>
                                <th class="check left"><input id="toggle" name="toggle" type="checkbox" style="margin-top: 8px;" onclick="checkAll(this);checkID();" /></th>
                                <th class="code left hidden">配信先コード</th>
                                <th class="mail left">配信先名</th>
                            </tr>
                         </thead>
                         
                         <tbody>';
        if(!empty($result)){
            foreach ($result as $key => $row){
                echo   '<tr>
                           <td class="check left">
                                <input class="checkbox-id" type="checkbox" id="cb'.$row->id.'" name="cid[]" value="'.$row->id.'" onclick="isChecked(this.checked);checkID('.$row->id.');" />                           
                           </td>
                           <td class="code left hidden" id="username'.$row->id.'">'.$row->username.'</td>
                           <td class="left"  id="user'.$row->id.'">'.$row->name.'</td>
                        </tr>';
            }
        } else {
            echo   '    <tr>
                            <td class="center" colspan="3">データは見つかりませんでした。</td>
                        </tr>';
        }
        
        echo '          </tbody>         
                    </table>
                      </div>
                      <input type="hidden" id="all_user" name="all_user" value="'.$s.'" />
                      <input type="hidden" name="task" value="" />
                      <input type="hidden" name="boxchecked" id="boxchecked" value="0"/>
                      
                      <textarea name="list_uid" id="list_uid"></textarea>
                      ';
        
        $app = &JFactory::getApplication();
        $app->close();
    }

    public function search_group_frmessage(){
        $result = $this->getModel()->getAllGroupMessage();
        
        echo '<div class="the_scroll">
                      <table>
                         <thead>
                            <tr>
                                <th class="check left"><input id="toggle" name="toggle" style="margin-top: 8px;" type="checkbox" onclick="checkAll(this);checkID();" /></th>
                                <th class="code left">配信先コード</th>
                                <th class="mail left">配信先名</th>
                            </tr>
                         </thead>
                         
                         <tbody>';
        if(!empty($result)){

            foreach ($result as $key => $row){
              
              $cls = 0;
                echo   '<tr class="row'.$cls.'">
                           <td class="check left">
                                <input class="checkbox-id" type="checkbox" id="cb'.$row->MAGR_CODE.'" name="cid[]" value="'.$row->MAGR_CODE.'" onclick="isChecked(this.checked);checkID('.$row->MAGR_CODE.');" />                           
                           </td>
                           <td class="code left" id="g_code'.$row->MAGR_CODE.'">'.$row->MAGR_CODE.'</td>
                           <td class="left" id="g_name'.$row->MAGR_CODE.'">'.$row->MAGR_NAME.'</td>
                        </tr>';
            }
        } else {
            echo   '    <tr>
                            <td class="center" colspan="3">データは見つかりませんでした。</td>
                        </tr>';
        }
        
        echo '          </tbody>         
                    </table>
                      </div>
                      <input type="hidden" id="all_user" name="all_user" value="'.$s.'" />
                      <input type="hidden" name="task" value="" />
                      <input type="hidden" name="boxchecked" id="boxchecked" value="0"/>
                      
                      <textarea name="list_uid" id="list_uid"></textarea>
                      ';
        
        $app = &JFactory::getApplication();
        $app->close();
    }

}